using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;

namespace JsonRpcGen.TypeHandler
{
    internal class EnumArrayTypeHandler : TypeHandler
    {
        private readonly JToken _enums;

        public EnumArrayTypeHandler(JToken enums)
        {
            _enums = enums;
        }

        public override void Write(StreamWriter writer, string namesp, string className, Dictionary<string, string> replacements)
        {
            writer.WriteLine("using System;");
            writer.WriteLine("using System.Collections.Generic;");
            writer.WriteLine("using System.Linq;");
            writer.WriteLine("namespace " + namesp);
            writer.WriteLine("{");
            writer.WriteLine("   public enum " + className +"Item");

            writer.WriteLine("   {");
            foreach (var eval in _enums)
            {
                var name = eval.ToString();
                if (Global.IsReservedName(name))
                {
                    writer.WriteLine("       [global::System.Runtime.Serialization.EnumMember(Value=\"" + name + "\")]");
                    name = Global.MakeFirstUpper(name);
                }
                writer.WriteLine("       " + name + ",");

            }
            writer.WriteLine("   }");
            writer.WriteLine("   public class " + className + " : List<" + className + "Item>");

            writer.WriteLine("   {");
            writer.WriteLine("         public static " + className + " AllFields()");
            writer.WriteLine("         {");
            writer.WriteLine("             var items = Enum.GetValues(typeof (" + className + "Item));");
            writer.WriteLine("             var list = new "+ className + "();");
            writer.WriteLine("             list.AddRange(items.Cast<" + className +"Item>());");
            writer.WriteLine("             return list;");
            writer.WriteLine("         }");
            writer.WriteLine("   }"); 
            writer.WriteLine("}");
        }
    }
}